/*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/
package com.feiyue.basic.server.parser;

import android.util.Xml;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

// Referenced classes of package com.feiyue.basic.server.parser:
//			Chapter, Base64

public class Xml2ParseChapterList
{

	private List ChapterList;
	private int bookId;
	private int categoryId;

	public Xml2ParseChapterList(String s, int i, int j)
		throws XmlPullParserException, IOException
	{
		ChapterList = new ArrayList();
		categoryId = i;
		bookId = j;
		XmlPullParser xmlpullparser = Xml.newPullParser();
		xmlpullparser.setInput(new ByteArrayInputStream(s.getBytes()), null);
		Parser(xmlpullparser);
	}

	private void Parser(XmlPullParser xmlpullparser)
		throws IOException, XmlPullParserException
	{
		Chapter chapter = null;
		
		int eventType = xmlpullparser.getEventType();
	    
	    while (eventType != XmlPullParser.END_DOCUMENT) {
	    	if (eventType == XmlPullParser.START_DOCUMENT) {
	    		//start parser
	    	} else if (eventType== XmlPullParser.START_TAG) {
	    		String tagName = xmlpullparser.getName();
	    		
	    		if ("chapterList".equals(tagName)) {
	    		} else if ("chapter".equals(tagName)) {
	    			chapter = new Chapter();
					chapter.setCategoryId(categoryId);
					chapter.setBookId(bookId);
	    		} else if ("id".equals(tagName)) {
	    			xmlpullparser.next();
					chapter.setId(Integer.valueOf(xmlpullparser.getText()).intValue());
	    		} else if ("name".equals(tagName)) {
	    			xmlpullparser.next();
					xmlpullparser.getText();
					String s;
					if (xmlpullparser.getText() == null)
						s = (new StringBuilder("第")).append(1 + ChapterList.size()).append("章").toString();
					else
						s = Base64.decode((new StringBuilder(String.valueOf(xmlpullparser.getText()))).toString(), "utf-8");
					chapter.setName(s);
	    		}
	    	} else if (eventType== XmlPullParser.END_TAG) {
	    		String tagName = xmlpullparser.getName();
	    		
	    		if ("chapterList".equals(tagName)) {
	    		} else if ("chapter".equals(tagName)) {
	    			ChapterList.add(chapter);
	    		} else if ("id".equals(tagName)) {
	    		} else if ("name".equals(tagName)) {
	    		}
	    	}
	    	
	    	eventType = xmlpullparser.next();
	    }
	}

	public List getChapterList()
	{
		return ChapterList;
	}

	public void setChapterList(List list)
	{
		ChapterList = list;
	}
}